<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {

	//系统首页
    public function index(){
    	 // 查询所有顶级分类
        $dingji = M('Category')->where('pid=0')->limit(2)->getField('id', true);

        // 随机取三个顶级分类
        $arr    = array_rand(array_flip($dingji), 3);
        $tmp    = [];

        // 把取到的三个顶级分类下面的子分类全部猎取出来
        foreach ($dingji as $v) {
            $tmp[] = $this->digui($v, 'id,title,pid');
        }

        // 把猎取到的子分类的id 分类放到数组中
        $whereArr = [];
        foreach ($tmp as $k => $v) {
            foreach ($v as $vv) {
                $whereArr[$k][] = $vv['id'];
            }
        }
        
        // 根据子分类的id 查询 商品 cate id数组当中的商品
        foreach ($whereArr as $k => $v) {
            $where['pid'] = ['in', $v];
            $goods[]       = M('goods')->where($where)->limit(8)->select();
        }

        $cate = D('Category')->getTree(0,'id,title,pid');
        $lists    = D('Document')->lists(null);
        $price = D('attr')->getfield('gid,price');
        $category = D('category')->getField('id,title');

        $this->assign('arr',$dingji);
        $this->assign('category',$category);
        $this->assign('goods',$goods);
        $this->assign('cate',$cate);//栏目
       // $this->assign('lists',$lists);//列表
       // $this->assign('page',D('Document')->page);//分页


                 
        $this->display();
    }
    public function introduction(){
    	$id = I('get.id');
    	$res = D('goods')->find($id);
    	$attrk = D('attrk')->where(['gid'=>$id])->select();
    	$attrv = D('attrv')->where(['gid'=>$id])->select();
    	//var_dump($attrk,$attrv);
    	foreach ($attrk as &$v) {
    		foreach ($attrv as $vv) {
    			if ($v['id']==$vv['kid']) {
    				$v['sb'][] = $vv;
    			}
    			
    		}
    	}
    	//var_dump($attrk);
    	$this->assign('attrk',$attrk);
    	$this->assign('res',$res);
    	$this->display();
    	
    }
    public function price(){
    	$where['gid'] = I('post.gid');
    	$where['attrvid'] =implode(',',I('post.attrvid'));
    	$res = D('attr')->where($where)->find();
    	if($res){
    		$res['error'] = '成功';
    		$res['status'] = '1';
    		$this->ajaxReturn($res);
    	}else{
    		$res['error'] = '错误';
    		$res['status'] = '0';
    		$this->ajaxReturn($res['error']);
    	}
    }
    public function search(){
    	$id = I('get.id');
  		$res = D('goods')->where(['pid'=>$id])->select();
  		$this->assign('res',$res);
    	$this->display();
    }

    public function digui($pid = 0, $field = true, $arr = [], $lv = 0)
    {
        $cate = M('Category')->where('pid=' . $pid)->field($field)->select();
        if ($cate) {
            $lv++;
            foreach ($cate as &$v) {
                $v['lv'] = $lv;
                $arr[]   = $v;
                $arr     = $this->digui($v['id'], $field, $arr, $lv);
            }
        }
        return $arr;
    }

}